﻿using AutoMapper;
using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using B.MES.Customer.Infrastructure.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using SqlSugar;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    /// <summary>
    /// BOM管理 读 控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BOMController : ControllerBase
    {
        /// <summary>
        ///  BOM应用层接口
        /// </summary>
        private readonly IBOMApplication _bOMApplication;
        /// <summary>
        /// 产品应用层接口
        /// </summary>
        private readonly IProductApplication _productApplication;
        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly ILogger<BOMController> _logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bOMApplication"> BOM应用层接口</param>
        /// <param name="logger"> 日志接口</param>
        public BOMController(IBOMApplication bOMApplication, ILogger<BOMController> logger, IProductApplication productApplication)
        {
            _bOMApplication = bOMApplication;
            _logger = logger;
            _productApplication = productApplication;
        }
        /// <summary>
        ///  添加 BOM 信息方法
        /// </summary>
        /// <param name="bom">要添加的 BOM 信息对象</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public IActionResult AddBom(BOMDto bom)
        {
            try
            {
                //判断Bon是否为空
                if (bom == null)
                {
                    return BadRequest();
                }
                return Ok(_bOMApplication.AddBom(bom));
            }
            catch (Exception ex)
            {
                _logger.LogError("添加 BOM 信息方法出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 绑定成品类型
        /// </summary>
        /// <returns>成品类型列表</returns>
        [HttpGet]
        public IActionResult GetArtifacTypeList()
        {
            try
            {
                return Ok(_bOMApplication.GetArtifacTypeList());
            }
            catch (Exception ex)
            {
                _logger.LogError("绑定成品类型方法出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 显示、分页、查询Bom列表
        /// </summary>
        /// <param name="bomTopic">Bom主题</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns>Bom列表信息</returns>
        [HttpGet]
        public IActionResult GetBom(string? bomTopic, int pageIndex, int pageSize)
        {
            try
            {
                return Ok(_bOMApplication.GetBom(bomTopic, pageIndex, pageSize));
            }
            catch (Exception ex)
            {
                _logger.LogError("显示、分页、查询Bom列表方法出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 反填Bom信息
        /// </summary>
        /// <param name="bomId">bom表主键</param>
        /// <returns>返回Bom实体</returns>
        [HttpGet]
        public IActionResult GetBomTabById(int bomId)
        {
            return Ok(_bOMApplication.GetBomTabById(bomId));
        }
        /// <summary>
        /// 修改Bom信息
        /// </summary>
        /// <param name="bom">要添加的 BOM 信息对象</param>
        /// <returns> 受影响行数</returns>
        [HttpPut]
        public IActionResult UpdBomInfoById(BomTab bom)
        {
            return Ok(_bOMApplication.UpdBomInfoById(bom));
        }
        /// <summary>
        /// 获取Bom详情
        /// </summary>
        /// <param name="bomId">bom表主键</param>
        /// <returns> 获取Bom详情</returns>
        [HttpGet]
        public IActionResult GetBomDetail(int bomId)
        {
            return Ok(_bOMApplication.GetBomDetail(bomId));
        }
        /// <summary>
        /// 获取BOM下的物料信息
        /// </summary>
        /// <param name="bomId"> BomId</param>
        /// <returns> 物料信息</returns>
        [HttpGet]
        public IActionResult GetMaterialsByBOMId(int bomId)
        {
            return Ok(_bOMApplication.GetMaterialsByBOMId(bomId));
        }
        /// <summary>
        /// 导出Bom列表
        /// </summary>
        /// <returns>Bom列表</returns>
        [HttpGet]
        public IActionResult ExportTab()
        {
            var list = _bOMApplication.GetBomList();
            var ms = new MemoryStream();
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (var package = new ExcelPackage())
            {
                var worksheet = package.Workbook.Worksheets.Add("Sheet1");

                worksheet.Workbook.Properties.SetCustomPropertyValue("Lauguage", "zh-CN");

                // 设置表头样式
                using (var range = worksheet.Cells["A1:H1"])
                {
                    range.Style.Font.Bold = true;
                    range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                }
                worksheet.Cells["A1"].Value = "序号";
                worksheet.Cells["B1"].Value = "BOM主题";
                
                worksheet.Cells["C1"].Value = "BOM编号";
                worksheet.Cells["D1"].Value = "版本";
                worksheet.Cells["E1"].Value = "成品类型";
                worksheet.Cells["F1"].Value = "日产量";
                worksheet.Cells["G1"].Value = "备注";

                for (int i = 0; i < list.Count; i++)
                {
                    worksheet.Cells["A" + (i + 2)].Value = i + 1;
                    worksheet.Cells["B" + (i + 2)].Value = list[i].BOMTopic;
                    
                    worksheet.Cells["C" + (i + 2)].Value = list[i].BOMNum;
                    worksheet.Cells["D" + (i + 2)].Value = list[i].Versions;
                    worksheet.Cells["E" + (i + 2)].Value = list[i].ArtifactType;
                    worksheet.Cells["F" + (i + 2)].Value = list[i].DailyOutput;
                    worksheet.Cells["G" + (i + 2)].Value = list[i].Remark;
                }

                package.SaveAs(ms);
            }
            ms.Seek(0, SeekOrigin.Begin);

            return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "Bom管理表单.xlsx"
            };
        }
        /// <summary>
        /// 删除Bom信息
        /// </summary>
        /// <param name="bomId">BomId</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public IActionResult DelBomById(int bomId)
        {
            return Ok(_bOMApplication.DelBomById(bomId));
        }
        /// <summary>
        /// 全局过滤器测试接口
        /// </summary>
        /// <returns>测试</returns>
        [HttpGet]
        public IActionResult TestLog()
        {
            try
            {
                BomTab bom = null;
                return Ok(bom.BOMTopic);
            }
            catch (Exception ex)
            {
                _logger.LogError("全局过滤器测试接口异常信息" + ex.Message);
                throw;
            }
        }
    }
}
